package ynu.edu.elmspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import ynu.edu.elmspringboot.mapper.FoodMapper;
import ynu.edu.elmspringboot.po.Food;
import ynu.edu.elmspringboot.service.FoodService;

@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Override
    public IPage<Food> getFoodPage(Page<Food> page, String foodName, Integer businessId) {
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        
        // 添加食品名称模糊查询条件
        if (StringUtils.hasText(foodName)) {
            queryWrapper.like("food_name", foodName);
        }
        
        // 添加商家ID查询条件
        if (businessId != null) {
            queryWrapper.eq("business_id", businessId);
        }
        
        // 按食品ID排序
        queryWrapper.orderByAsc("food_id");
        
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<Food> getFoodPageByBusiness(Page<Food> page, Integer businessId) {
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        queryWrapper.orderByAsc("food_id");
        
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<Food> getFoodPageByPriceRange(Page<Food> page, Double minPrice, Double maxPrice) {
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        
        // 添加价格范围查询条件
        if (minPrice != null && maxPrice != null) {
            queryWrapper.between("food_price", minPrice, maxPrice);
        } else if (minPrice != null) {
            queryWrapper.ge("food_price", minPrice);
        } else if (maxPrice != null) {
            queryWrapper.le("food_price", maxPrice);
        }
        
        // 按价格排序
        queryWrapper.orderByAsc("food_price");
        
        return this.page(page, queryWrapper);
    }
}
